package cn.fl.warrant.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.warrant.service.IArcMgrMainService;
import cn.fl.warrant.service.IArcSwitchMainService;
import cn.fl.warrant.transform.ArcSwitchMainVOTransform;
import cn.fl.warrant.transform.ArcSysFlowSHVOTransform;
import cn.fl.warrant.vo.ArcApplyDetailVO;
import cn.fl.warrant.vo.ArcMgrDetailVO;
import cn.fl.warrant.vo.ArcSwitchDetailVO;
import cn.fl.warrant.vo.ArcSwitchMainVO;
import cn.fl.warrant.vo.ArcSysFlowSHVO;
import fl.flow.facade.flow.api.IFlowApiService;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.dto.SysFlowSHDTO;
import fl.warrant.facade.archive.api.IArcSwitchMainApiService;
import fl.warrant.facade.archive.condition.ArcSwitchMainQC;
import fl.warrant.facade.archive.constant.ArchiveConstants;
import fl.warrant.facade.archive.dto.ArcSwitchMainDTO;

/**
 * @Description: 转仓主表客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class ArcSwitchMainServiceImpl implements IArcSwitchMainService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IArcSwitchMainService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IArcSwitchMainApiService arcSwitchMainApiService;
    /**
     * 流程服务
     */
    @Autowired
    private IFlowApiService flowApiService;
    @Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
    @Autowired
    private ISysUsrApiService sysUsrApiService;
    @Autowired
	private ISysOrgApiService sysOrgApiService;// 组织机构
    @Autowired
    private IArcMgrMainService arcMgrMainService;
    
    
	@Override
	public ArcSwitchMainVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		ArcSwitchMainDTO dto = this.arcSwitchMainApiService.selectById(id);
		
		return ArcSwitchMainVOTransform.toVO(dto);
	}

	@Override
	public Long add(ArcSwitchMainVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		return this.arcSwitchMainApiService.add(ArcSwitchMainVOTransform.toDTO(entity));
		
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.arcSwitchMainApiService.removeById(id);
		
	}
	
	

	@Override
	public boolean modify(ArcSwitchMainVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.arcSwitchMainApiService.modify(ArcSwitchMainVOTransform.toDTO(entity));
		
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		return this.arcSwitchMainApiService.searchListPage(page);
		
	}

	/**
	 * 查找归档申请流程
	 */
	@Override
	public List<ArcSysFlowSHVO> searchSwitchFlow(ArcSwitchMainQC condition) throws BaseException{
//		ArcSwitchMainQC condition=new ArcSwitchMainQC();
//		condition.setContractNo(contractNo);
		if(null == condition){
			BaseLogUtils.error(logger, "searchSwitchFlow", "参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		try {
			
			List<ArcSwitchMainDTO> list = (List<ArcSwitchMainDTO>) this.arcSwitchMainApiService.searchList(condition);
			List<SysFlowSHDTO> searchList=new ArrayList<SysFlowSHDTO>();
			if (list!=null) {
				for (int i = 0; i < list.size(); i++) {
					SysFlowSHQueryCondition qc = new SysFlowSHQueryCondition();
					qc.setSysCd(CurrentThreadContext.getCurrentSysCd());//系统代码
					qc.setWordsInTheTable(ArchiveConstants.ARC_FLOW_S_H_ARC);//表中字
					qc.setDataId(list.get(i).getPkid());//业务主键
					//qc.setpDefKey(flowCompleteParamsDTO.getProcDefKey());
					qc.setStatus(BaseConstant.IS_YESNO_YES);
					List<SysFlowSHDTO> sList = this.flowApiService.searchFlowSHDTOList(qc);
					searchList.addAll(sList);
				}
			}
			List<ArcSysFlowSHVO> rList=ArcSysFlowSHVOTransform.toVOList(searchList);
			for (int i = 0; i <rList.size(); i++) {
				ArcSysFlowSHVO vo=rList.get(i);
				List<KeyValueVO>  list1= this.sysDictionaryApiService.getKvList(ArchiveConstants.FLOW_STATUS, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
				if (StringUtils.isBlank(vo.getAudStsCdNm())&&StringUtils.isNotBlank(vo.getAudStsCd())) {
					for (int j = 0; j <list1.size(); j++) {
						if (vo.getAudStsCd().equals(list1.get(j).getKey())) {
							vo.setAudStsCdNm(list1.get(j).getValue());
						}
					}
				}
				if (vo.getCrtUsrId()!=null) {
					SysUsrDTO dto=this.sysUsrApiService.selectSysUsrById(vo.getCrtUsrId(), CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
					if (dto!=null) {
						vo.setCrtUsrIdNm(dto.getUsrNm());
					}
				}
			}
			return rList;
			
		} catch (Exception e) {
			// TODO: handle exception
			BaseLogUtils.error(logger, "searchSwitchFlow", "系统错误"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
			return null;
		}
	}
	
	@Override
	public List<ArcSwitchMainVO> searchList(ArcSwitchMainQC qc) {
		// TODO Auto-generated method stub
		if(null == qc) return null;
		List<ArcSwitchMainVO> list=ArcSwitchMainVOTransform.toVOList(this.arcSwitchMainApiService.searchList(qc));
		if(CollectionUtils.isNotEmpty(list)) {
			for (ArcSwitchMainVO svo:list) {
				if (svo.getDlist()!=null&&svo.getDlist().size()>0) {			
					this.getFiles(svo.getDlist());
				}
			}
		}
		return list;
	}

	@Override
	public List<ArcSwitchMainVO> searchListForFlow(ArcSwitchMainQC qc) {
		// TODO Auto-generated method stub
		if(null == qc) return null;
		List<ArcSwitchMainVO> list= ArcSwitchMainVOTransform.toVOList(this.arcSwitchMainApiService.searchListForFlow(qc));
		for (ArcSwitchMainVO svo:list) {
			if (svo.getDlist()!=null&&svo.getDlist().size()>0) {			
				this.getFiles(svo.getDlist());
			}
		}
		
		return list;
	}

	@Override
	public DataResultDTO addFlow(List<ArcSwitchMainVO> entity) throws BaseException {
		// TODO Auto-generated method stub
		return this.arcSwitchMainApiService.addFlow(ArcSwitchMainVOTransform.toDTOList(entity));
	}
	public void getFiles(List<ArcSwitchDetailVO> list2) {
		
		List<ArcMgrDetailVO> list1=new ArrayList<ArcMgrDetailVO>() ;
		for (ArcSwitchDetailVO dvo:list2) {
			ArcMgrDetailVO vo=new ArcMgrDetailVO();
			vo.setArcReceiptDetailId(dvo.getArcReceiptDetailId());
			vo.setContractNo(dvo.getContractNo());
			vo.setPaylistCode(dvo.getPaylistCode());
			vo.setFileName(dvo.getFileName());
			vo.setFileFlag(dvo.getFileFlag());
			vo.setEleId(dvo.getEleId());
			list1.add(vo);
		}
		//获取附件
		this.arcMgrMainService.getFilSign(list1);
		this.arcMgrMainService.getFiles(list1);
		for (ArcMgrDetailVO vo:list1) {
			for (ArcSwitchDetailVO avo:list2) {
				if (avo.getArcReceiptDetailId().equals(vo.getArcReceiptDetailId())) {
					avo.setEleId(vo.getEleId());
					avo.setFilePath(vo.getFilePath());
					avo.setUlFiles(vo.getUlFiles());
					//avo.setUlFlag(vo.getUlFlag());
					if (vo.getBz()!=null) {
						avo.setBz(vo.getBz());
					}					
					break;
				}
			}
		}	
	}
}
